package com.itheima.ydd.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.BusinessException;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.common.UserHolder;
import com.itheima.ydd.entity.YddUser;
import com.itheima.ydd.mapper.YddUserMapper;
import com.itheima.ydd.service.IYddArticleService;
import com.itheima.ydd.service.IYddUserDataService;
import com.itheima.ydd.service.IYddUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.HashMap;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2022-05-22
 */
@Service
public class YddUserServiceImpl extends ServiceImpl<YddUserMapper, YddUser> implements IYddUserService {


    @Autowired
    private IYddUserDataService yddUserDataService;

    @Autowired
    private IYddArticleService yddArticleService;

    @Override
    public ResultData pageList(String sex, String username, String mobile, String page, String limit) {
        Page<YddUser> pageInfo = new Page<>(Long.valueOf(page), Long.valueOf(limit));

        LambdaQueryWrapper<YddUser> wrapper = new LambdaQueryWrapper<>();
        //根据name进行模糊查询
        wrapper.like(!StringUtils.isEmpty(username), YddUser::getUsername, username);
        //根据电话号码进行模糊查询
        wrapper.like(!StringUtils.isEmpty(mobile), YddUser::getMobile, mobile);
        //根据性别进行模糊查询
        wrapper.like(!StringUtils.isEmpty(sex),YddUser::getSex,sex);
        wrapper.eq(YddUser::getDefaultData, 1);

        Page<YddUser> page2 = page(pageInfo, wrapper);
        //执行分页查询
        HashMap<Object, Object> map = new HashMap<>();
        map.put("count", page2.getCountId());
        map.put("pageSize", page2.getSize());
        map.put("currentPage", page2.getCurrent());
        map.put("data", page2.getRecords());
        map.put("count", page2.getTotal());

        return ResultData.ok("查询成功", map);
    }
    @Override
    public void delById(String id) {
        if (id == null) {
            throw new BusinessException("未选中要操作的数据");
        }
        LambdaUpdateWrapper<YddUser> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(YddUser::getDefaultData, 0).eq(YddUser::getId, Integer.valueOf(id));
        update(wrapper);
    }

    @Override
    public void addUser(YddUser yddUser) {
        String mobile = yddUser.getMobile();
        LambdaQueryWrapper<YddUser> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(YddUser::getMobile,mobile);
        YddUser one=getOne(queryWrapper);
        if (one!=null){
            throw new BusinessException("该手机号已绑定其他账号");
        }else {
            yddUser.setDefaultData(String.valueOf(1));
            yddUser.setPassword(DigestUtils.md5DigestAsHex(yddUser.getPassword().getBytes()));
            save(yddUser);
        }
    }

    @Override
    public ResultData update(YddUser yddUser) {
        Integer id = yddUser.getId();
        LambdaQueryWrapper<YddUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(YddUser::getId, id);
        YddUser one = getOne(wrapper);
        //根据用户要修改的手机号判断

        //如果手机号是本机手机号则修改成功


        String mobile = yddUser.getMobile();
        LambdaQueryWrapper<YddUser> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(YddUser::getMobile, mobile);
        YddUser one1 = getOne(wrapper1);
        if ((one != null && yddUser.getMobile().equals(one.getMobile()))) {
            updateById(yddUser);
            return ResultData.ok("修改成功");
        } else {
            throw new BusinessException("手机号已被其他用户注册");
        }
    }

    @Override
    public YddUser rowInfo(String id) {
        if (id==null){
            throw new BusinessException("未选中操作数据");
        }
        return getById(id);
    }

    /*@Override
    public ResultData getUserInfo() {
        String getUserInfo = UserHolder.getUserId();
        YddUser byId = getById(Integer.valueOf(getUserInfo));
        Integer id = byId.getId();
        //收藏
        Integer collectNum = yddUserDataService.getnum(id, 1);
        //点赞
        Integer zanNum = yddUserDataService.getnum(id, 2);

        Integer getarticle = yddArticleService.getarticle(id);

        HashMap<Object, Object> map = new HashMap<>();
        map.put("articleNum", getarticle);
        map.put("collectNum", collectNum);
        map.put("zanNum", zanNum);
        map.put("userInfo", byId);
        return ResultData.ok(map);
    }
*/

}
